//
//  HomeWeatherModel.m
//  QuickCure1
//
//  Created by Ashzenki on 02/11/2016.
//  Copyright © 2016 maoge. All rights reserved.
//

#import "HomeWeatherModel.h"

@implementation HomeWeatherModel

- (NSString *)description {
    
    return [self yy_modelDescription];
}


- (void)getWeatherDataWithCity:(NSString *)cityName complete:(void(^)(HomeWeatherModel *model, NSError *error))completeBlock {
    
    NSAssert(completeBlock != nil, @"完成block不能为空");
    
    NSDictionary *paramDic = @{@"location":cityName,
                               @"output":@"json",
                               @"ak":@"17IvzuqmKrK1cGwNL6VQebF9"
                               };
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    [manager GET:@"http://api.map.baidu.com/telematics/v3/weather" parameters:paramDic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary * _Nullable responseObject) {
        NSLog(@"%@", responseObject);
        //如果获取成功
        if([responseObject[@"status"] isEqualToString:@"success"]){
            
            NSDictionary *resultsDict = [(NSArray *)responseObject[@"results"] firstObject];
            
            //获取天气数据
            NSDictionary *weatherDict = [(NSArray*)resultsDict[@"weather_data"] firstObject];
            
            //获取健康数据
            NSDictionary *healthDict = [(NSArray*)resultsDict[@"index"] objectAtIndex:3];

//            NSLog(@"%@", healthDict);
            
            [self yy_modelSetWithJSON:weatherDict];
            [self yy_modelSetWithJSON:healthDict];
        }
        
        completeBlock(self, nil);
                
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"获取天气信息失败 %@", error);
        
        completeBlock(nil, error);
    }];
}

@end



































